/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.dao;


import cz.muni.entities.Customer;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author OSiRiS-PC
 */
@Stateless
public class CustomerDao implements CustomerDaoLocal {

    
    @PersistenceContext
    private EntityManager em;
    
   
    @Override
    public void create(Customer customer) {
        System.out.println("customer dao create metoda");
        em.persist(customer);
    }

    @Override
    public List<Customer> findByFirstName(String firstName) {
        
        Query q = em.createQuery ("SELECT x FROM Customer x WHERE x.firstName = ?1");
        q.setParameter(1, firstName);
       
        List<Customer> results = (List<Customer>) q.getResultList ();
        return results;
    }

    @Override
    public List<Customer> findByLastName(String lastName) {
        Query q = em.createQuery ("SELECT x FROM Customer x WHERE x.lastName = ?1");
        q.setParameter(1, lastName);
        List<Customer> results = (List<Customer>) q.getResultList ();
        return results;
    }

    @Override
    public List<Customer> findByEmail(String email) {
        Query q = em.createQuery ("SELECT x FROM Customer x WHERE x.email = ?1");
        q.setParameter(1, email);
        List<Customer> results = (List<Customer>) q.getResultList ();
        return results;
    }

    @Override
    public List<Customer> findByPhoneNumber(String number) {
        
        Query q = em.createQuery ("SELECT x FROM Customer x WHERE x.phoneNumber = ?1");
        q.setParameter(1, number);
        List<Customer> results = (List<Customer>) q.getResultList ();
        return results;
    }

  
    @Override
    public void delete(Customer customer) {
        customer = em.merge(customer);
        em.remove(customer);
    }

    @Override
    public Customer find(Long id) {
        return em.find(Customer.class, id);
    }

    @Override
    public Customer update(Customer customer) {
        customer = em.merge(customer);
        em.persist(customer);
        return customer;
    }

    @Override
    public List<Customer> findAll() {
         return em.createQuery("from " + Customer.class.getName(), Customer.class).getResultList();
    }

   

  

}
